import time

from selenium import webdriver
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.Common.do_mysql import DoMysql
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.Test_data.organization_data import Organization
from Case.python_ui.Common.do_mysql import DoMysql
from Case.python_ui.PageLocate.user_locator import UserLocator
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException

class  OrganizationPage(Basepage):

        """
                重制查询条件
        """

        # 重制查询条件
        def refresh_search(self):
                doc = '重置筛查询条件'
                self.wait_elevisble(OrganizationLocator.organization_query_input, doc)
                self.double_click(OrganizationLocator.organization_query_input, doc)
                self.keyboard_event_all_select(OrganizationLocator.organization_query_input, doc)
                self.keyboard_event(OrganizationLocator.organization_query_input, doc)
                time.sleep(1)
                self.move_mouse_to_click_element(OrganizationLocator.organization_query_button, doc)
                time.sleep(5)

        # 重制查询条件 member
        def refresh_search_member(self):
                doc = '重置筛查询条件'
                time.sleep(2)
                self.wait_elevisble(OrganizationLocator.immediate_member_search_input, doc)
                self.click_element(OrganizationLocator.immediate_member_search_input, doc)
                self.keyboard_event_all_select(OrganizationLocator.immediate_member_search_input,doc)
                #self.double_click(OrganizationLocator.immediate_member_search_input, doc)
                self.keyboard_event(OrganizationLocator.immediate_member_search_input, doc)
                time.sleep(1)
                self.move_mouse_to_click_element(OrganizationLocator.immediate_member_query_button, doc)
                time.sleep(5)


        """"""

        #进入到组织页面
        def   enter_the_organization_management_page(self,cmdopt):
                time.sleep(5)
                doc='进入到organization_management页面'
                time.sleep(5)
                for  i  in range(3):
                     try:
                         self.wait_elevisble(OrganizationLocator.user_management_tab,doc)
                         self.move_mouse_to_click_element(OrganizationLocator.user_management_tab,doc)
                         time.sleep(2)
                         self.wait_elevisble(OrganizationLocator.organization_tab,doc)
                         self.move_mouse_to_click_element(OrganizationLocator.organization_tab,doc)
                         break
                     except:
                         self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                         time.sleep(5)
                         continue
                time.sleep(2)
                self.refresh_search()
                self.reset_filtering()
                self.wait_elevisble(OrganizationLocator.page_total,doc)


        def reset_filtering(self):
                doc = '重置筛选项'
                try:
                        time.sleep(2)
                        self.wait_elevisble(UserLocator.status_filter, doc)
                        time.sleep(2)
                        self.move_mouse_to_click_element(UserLocator.status_filter, doc)
                        time.sleep(2)
                        self.wait_elevisble(OrganizationLocator.filter_reset_button, doc)
                        time.sleep(2)
                        self.click_element(OrganizationLocator.filter_reset_button, doc)
                        time.sleep(1)
                        self.click_element(OrganizationLocator.filter_ok, doc)
                        time.sleep(1)
                except:
                        pass



        #校验页面
        def get_account_email(self,):
                doc='获取账号'
                time.sleep(5)
                for i in range(3):
                        try:
                                self.wait_elevisble(OrganizationLocator.account_email_tab,doc)
                                break
                        except:
                                self.click_element(OrganizationLocator.reload_button,doc)
                                time.sleep(5)
                                continue
                account_email = self.get_element(OrganizationLocator.account_email_tab,doc).text
                return account_email


        def  wait_add_sub_deparment(self,cmdopt):
                doc='等待organization页面的加载'
                time.sleep(5)
                for i in range(3):
                        try:
                                self.wait_elevisble(OrganizationLocator.add_sub_deparment,doc)
                                self.click_element(OrganizationLocator.add_sub_deparment,doc)
                                self.wait_elevisble(OrganizationLocator.sub_deparment_name,doc)
                                break
                        except:
                                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                                self.enter_the_organization_management_page(cmdopt)
                                continue

        #新增组织
        def  add_sub_departmen(self,cmdopt):
                doc="新建组织"
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                time.sleep(2)
                self.wait_add_sub_deparment(cmdopt)
                org_name=CommonTool.generate_string()
                self.send_keys(OrganizationLocator.sub_deparment_name,org_name,doc)
                time.sleep(5)
                self.click_element(OrganizationLocator.confirm_add_sub_department,doc)
                time.sleep(2)
                self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)
                self.click_element(OrganizationLocator.organization_query_button,doc)
                self.wait_eleinvisble(OrganizationLocator.loading_wait,doc)
                self.send_keys(OrganizationLocator.organization_query_input,org_name,doc)
                self.click_element(OrganizationLocator.organization_query_button,doc)
                time.sleep(5)
                # self.wait_elevisble(OrganizationLocator.sub_department_name,doc)
                # self.click_element(OrganizationLocator.org_right_name,doc)
                time.sleep(5)
                res=self.get_element(OrganizationLocator.non_existent_organization_name,doc).text
                print(res)
                return  org_name,res



        def edit_department_info(self,org_name,cmdopt):
                doc='编辑组织信息'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(org_name)
                self.wait_elevisble(OrganizationLocator.action_list,doc)
                self.move_mouse_to_click_element(OrganizationLocator.action_list,doc)
                time.sleep(4)
                for i in range(3):
                        try:
                                self.wait_elevisble(OrganizationLocator.action_edit, doc)
                                break
                        except TimeoutException as e:
                                time.sleep(2)
                                self.move_mouse_to_click_element(OrganizationLocator.action_list, doc)
                                time.sleep(2)
                                self.save_screen('异常中再次点击action')
                time.sleep(2)
                self.move_mouse_to_click_element(OrganizationLocator.action_edit,doc)
                edit_org_name=CommonTool.generate_edit_org_name()
                self.wait_elevisble(OrganizationLocator.edit_name,doc)
                self.keyboard_event(OrganizationLocator.edit_name,doc)
                self.send_keys(OrganizationLocator.edit_name,edit_org_name,doc)
                time.sleep(2)
                self.click_element(OrganizationLocator.edit_confirm,doc)
                self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)
                return edit_org_name


        #组织成员页面校验

        def  check_member_detial(self,cmdopt):
                self.enter_the_organization_management_page(cmdopt)



        def check_query_organization_list(self,org_name):
                doc='查询组织列表'
                #self.refresh_page()
                #self.wait_elevisble(OrganizationLocator.organization_query_input)
                #self.send_keys(OrganizationLocator.organization_query_input,org_name)
                self.click_element(OrganizationLocator.organization_query_button,doc)
                time.sleep(3)
                self.wait_elevisble(OrganizationLocator.org_right_name,doc)
                time.sleep(2)
                org_right_name=self.get_element(OrganizationLocator.org_right_name,doc).text
                return  org_right_name

        def check_non_existent_organization(self,cmdopt):
                doc='查询不存在的组织'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.wait_elevisble(OrganizationLocator.organization_query_input,doc)
                self.send_keys(OrganizationLocator.organization_query_input,Organization.non_existent_organization['search_data'],doc)
                time.sleep(5)
                self.move_mouse_to_click_element(OrganizationLocator.organization_query_button,doc)
                self.wait_elevisble(OrganizationLocator.non_existent_organization_name,doc)
                non_existent_organization=self.get_element(OrganizationLocator.non_existent_organization_name,doc).text
                return  non_existent_organization


        def check_immediate_member_email_query(self,cmdopt):
                doc='查询组织下的成员'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(Organization.organization_name[cmdopt])
                self.wait_elevisble(OrganizationLocator.immediate_email,doc)
                eles=self.get_elements(OrganizationLocator.immediate_email,doc)
                list=[]
                for ele in eles:
                        list.append(ele.text)
                return list

        def check_nonexsitent_immediate_member_email_query(self,cmdopt):
                doc='查询组织下不存在的成员'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.wait_elevisble(OrganizationLocator.immediate_member_search_input,doc)
                self.send_keys(OrganizationLocator.immediate_member_search_input,Organization.non_existent_email,doc)
                time.sleep(5)
                self.click_element(OrganizationLocator.immediate_member_query_button,doc)
                #self.wait_elevisble(OrganizationLocator.loading_wait)
                self.wait_eleinvisble(OrganizationLocator.loading_wait,doc)
                res=self.get_elements(OrganizationLocator.immediate_email,doc)
                return  res


        def filtering_function_retrieval(self,cmdopt):
                doc='筛选状态查询'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                time.sleep(2)
                self.refresh_search_member()
                self.query_immediate_member(Logindata.user_data[cmdopt]['user']['username'])
                time.sleep(5)
                self.move_mouse_to_click_element(OrganizationLocator.filter,doc)
                time.sleep(2)
                self.click_element(OrganizationLocator.filter_normal,doc)
                time.sleep(2)
                self.click_element(OrganizationLocator.filter_ok,doc)
                res=self.get_element(OrganizationLocator.filter_status,doc).text
                return res




        def delete_sub_departmen(self,org_name,cmdopt):
                doc='删除组织'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(org_name)
                time.sleep(5)
                self.move_mouse_to_click_element(OrganizationLocator.action_list,doc)
                self.save_screen('点击action')
                time.sleep(2)
                for i in range(3):
                        try:
                                self.wait_elevisble(OrganizationLocator.action_delete,doc)
                                break
                        except TimeoutException as e:
                                time.sleep(2)
                                self.move_mouse_to_click_element(OrganizationLocator.action_list, doc)
                                time.sleep(2)
                                self.save_screen('异常中再次点击action')
                time.sleep(2)
                self.click_element(OrganizationLocator.action_delete,doc)
                time.sleep(2)
                self.wait_elevisble(OrganizationLocator.action_delete_confirm,doc)
                self.click_element(OrganizationLocator.action_delete_confirm,doc)
                time.sleep(2)
                self.wait_elevisble(OrganizationLocator.delete_success_prompt,doc)




        def get_status(self):
             doc='获取状态'
             self.wait_elevisble(OrganizationLocator.filter,doc)
             time.sleep(3)
             self.click_element(OrganizationLocator.filter,doc)
             time.sleep(5)
             eles=self.get_elements(OrganizationLocator.status,doc)
             status_list=[]
             for ele in eles:
                   status_list.append(ele.text)
             print(status_list)
             return status_list


        def get_user_name(self):
                doc='获取username'
                self.wait_elevisble(OrganizationLocator.account_email_tab,doc)
                time.sleep(2)
                user_name = self.get_element(OrganizationLocator.account_email_tab,doc).text
                return user_name

        def get_phone_number(self):
                doc='获取phone_number'
                self.wait_elevisble(OrganizationLocator.phone_number_tab,doc)
                phone_number=self.get_element(OrganizationLocator.phone_number_tab,doc).text
                return phone_number

        def get_creation_time(self):
                doc='获取创建时间'
                self.wait_elevisble(OrganizationLocator.creation_time_tab,doc)
                creation__time=self.get_element(OrganizationLocator.creation_time_tab,doc).text
                return creation__time


        def get_oreations(self):
                doc='获取操作类型'
                self.click_element(OrganizationLocator.reload_button,doc)
                self.wait_elevisble(OrganizationLocator.operation_button,doc)
                time.sleep(3)
                self.move_mouse_to_click_element(OrganizationLocator.operation_button,doc)
                time.sleep(5)
                eles=self.get_elements(OrganizationLocator.operations,doc)
                operation_list=[]
                for ele in eles:
                        operation_list.append(ele.text)
                print(operation_list)
                return operation_list


        def query_immediate_member(self,account_email):
                doc='查询组织下面的成员'
                self.wait_elevisble(OrganizationLocator.immediate_member_search_input,doc)
                self.send_keys(OrganizationLocator.immediate_member_search_input,account_email,doc)
                self.click_element(OrganizationLocator.immediate_member_query_button,doc)
                #self.wait_elevisble(OrganizationLocator.loading_wait,doc)
                self.wait_eleinvisble(OrganizationLocator.loading_wait,doc)
                time.sleep(2)


        def query_organization(self,org_name):
                doc='查询组织'
                self.wait_elevisble(OrganizationLocator.organization_query_input,doc)
                self.keyboard_event(OrganizationLocator.organization_query_input,doc)
                self.send_keys(OrganizationLocator.organization_query_input,org_name,doc)
                time.sleep(5)
                self.click_element(OrganizationLocator.organization_query_button,doc)
                time.sleep(5)


        def reset_org_user_pwd(self,org_name,email,cmdopt,get_database_source):
                doc='重置组织下面成员的密码'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(org_name)
                time.sleep(5)
                self.wait_elevisble(OrganizationLocator.reset_pwd,doc)
                self.move_mouse_to_click_element(OrganizationLocator.reset_pwd,doc)
                time.sleep(2)
                self.wait_elevisble(UserLocator.change_pwd_send,doc)
                self.click_element(UserLocator.change_pwd_send,doc)
                time.sleep(2)
                change_pwd_verification_code = \
                BusinessCommonTools().get_verification_code(cmdopt,email,get_database_source)
                time.sleep(2)
                self.send_keys(UserLocator.change_password_verificationcode_input, change_pwd_verification_code,doc)
                time.sleep(2)
                self.send_keys(UserLocator.new_pwd, Logindata.reset_pwd,doc)
                time.sleep(2)
                self.send_keys(UserLocator.confirm_new_pwd, Logindata.reset_confirm_pwd,doc)
                time.sleep(2)
                self.click_element(UserLocator.reset_pwd_ok,doc)
                time.sleep(2)
                self.wait_elevisble(Loginlocator.login_username_ele,doc)


        def admin_reset_org_user_pwd(self,org_name,cmdopt):
                doc = '主账号重置组织下面成员的密码'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(org_name)
                time.sleep(5)
                self.wait_elevisble(OrganizationLocator.reset_pwd, doc)
                self.move_mouse_to_click_element(OrganizationLocator.reset_pwd, doc)
                time.sleep(3)
                self.wait_elevisble(OrganizationLocator.delete_org_ack,doc)
                self.click_element(OrganizationLocator.delete_org_ack,doc)
                time.sleep(2)
                self.click_element(OrganizationLocator.edit_org_confirm,doc)
                self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)




        def edit_org_user_org_info(self,org_name,cmdopt):
                doc='编辑组织下面成员的组织'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(org_name)
                time.sleep(5)
                self .wait_elevisble(OrganizationLocator.operation_button,doc)
                self.move_mouse_to_click_element(OrganizationLocator.operation_button,doc)
                time.sleep(2)
                self.get_operation_name_click('Edit Organization')
                time.sleep(5)
                self.wait_elevisble(OrganizationLocator.edit_org_select,doc)
                self.click_element(OrganizationLocator.edit_org_select,doc)
                time.sleep(2)
                self.send_keys(OrganizationLocator.edit_org_input,Organization.default_organization[cmdopt]['user'],doc)
                eles=self.get_elements(OrganizationLocator.edit_org_to_default,doc)
                eles[0].click()
                time.sleep(2)
                self.click_element(OrganizationLocator.edit_org_confirm,doc)
                self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)


        def edit_org_user_user_group(self,org_name,cmdopt):
                doc='编辑组织成员下的用户组'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(org_name)
                self.wait_elevisble(OrganizationLocator.operation_button,doc)
                self.click_element(OrganizationLocator.operation_button,doc)
                time.sleep(2)
                self.get_operation_name_click('Edit User Group')
                time.sleep(5)
                self.click_element(OrganizationLocator.edit_org_select,doc)
                time.sleep(2)
                #self.send_keys(OrganizationLocator.edit_org_user_group_input,usergroup)
                eles = self.get_elements(OrganizationLocator.edit_user_group,doc)
                eles[0].click()
                time.sleep(2)
                self.click_element(OrganizationLocator.edit_org_select,doc)
                self.click_element(OrganizationLocator.edit_org_confirm,doc)
                self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)


        def  edit_org_user_user_role(self, org_name, role_name,cmdopt):
                doc='编辑组织下面的成员的角色'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(org_name)
                time.sleep(5)
                self.wait_elevisble(OrganizationLocator.operation_button,doc)
                self.move_mouse_to_click_element(OrganizationLocator.operation_button,doc)
                time.sleep(2)
                self.get_operation_name_click('Edit Role')
                time.sleep(5)
                self.click_element(OrganizationLocator.edit_org_role_select,doc)
                time.sleep(2)
                #eles = self.get_elements(OrganizationLocator.select_choices, doc)
                #eles[-1].click()
                self.scroll_bar_operation2(OrganizationLocator().get_edit_org_user_role(role_name),scroll_lines=5,down=2)
                time.sleep(2)
                self.click_element(OrganizationLocator().get_edit_org_user_role(role_name), doc)
                time.sleep(2)
                time.sleep(2)
                self.click_element(OrganizationLocator.edit_org_confirm,doc)
                self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)


        def resend_org_user(self,org_name,cmdopt):
                doc='重新激活成员'
                time.sleep(5)
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                self.enter_the_organization_management_page(cmdopt)
                self.query_organization(org_name)
                time.sleep(2)
                self.wait_elevisble(OrganizationLocator.operation_button,doc)
                self.click_element(OrganizationLocator.operation_button,doc)
                time.sleep(2)
                self.get_operation_name_click('Resend')
                time.sleep(2)
                self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)



        def get_operation_name_click(self, operation):
                doc='通过操作的名称去点击'
                eles = self.get_elements(OrganizationLocator.operation_type,doc)
                for ele in eles:
                        if ele.text == operation:
                                time.sleep(2)
                                ele.click()



